package com.senlingspace.quartz.task;

import com.alibaba.fastjson2.JSON;
import com.senlingspace.common.enums.*;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.system.domain.SlkjOrder;
import com.senlingspace.system.domain.SlkjOrderStatusLog;
import com.senlingspace.system.mapper.SlkjOrderMapper;
import com.senlingspace.system.mapper.SlkjOrderStatusLogMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单过期任务
 *
 * @author lebronjames
 * @date 2024/08/14
 */
@Slf4j
@AllArgsConstructor
@Component
public class OrderExpireTask {

    private final SlkjOrderMapper slkjOrderMapper;
    private final SlkjOrderStatusLogMapper slkjOrderStatusLogMapper;


    @Scheduled(cron = "0 0 23 * * ?") // 每天23点执行一次 将订单状态为已领设备的改为已完成
    @Transactional(rollbackFor = Exception.class)
    public void compensateOrderStatus() {
        log.info("23点 开始将订单状态为已领设备的改为已完成");
        int i = slkjOrderMapper.updateStatus();
        log.info("将订单状态为已领设备的改为已完成，共修改 " + i + "条");
    }


    @Scheduled(fixedDelay = 100000) // 每10秒钟执行一次
    @Transactional(rollbackFor = Exception.class)
    public void expireOrder() {
        log.info("订单过期任务执行开始");
        List<SlkjOrder> orders = slkjOrderMapper.expireOrderScan(15);
        if (CollectionUtils.isEmpty(orders)) {
            log.info("订单过期任务执行完成，没有需要处理的订单");
            return;
        }

        List<SlkjOrderStatusLog> statusLogs = new ArrayList<>();
        SlkjOrderStatusLog orderStatusLog;
        for (SlkjOrder order : orders) {
            String businessStatus = order.getBusinessStatus();
            String goodsStatus = order.getGoodsStatus();
            if ((businessStatus != null && businessStatus.equals(OrderBusinessStatus.ORDER_CANCELLED.getCode())) ||
                    (goodsStatus != null && goodsStatus.equals(GoodsStatusEnum.ORDER_CANCELLED.name()))) {
                continue;
            }

            log.info("订单过期任务执行，订单ID：{}", order.getOrderId());
            switch (OrderType.fromCode(order.getOrderType())) {
                case OT_101:
                    order.setBusinessStatus(OrderBusinessStatus.ORDER_CANCELLED.getCode());
                    break;
                case OT_102:
                    order.setGoodsStatus(GoodsStatusEnum.ORDER_CANCELLED.name());
                    break;
                case OT_103:
                    order.setBusinessStatus(OrderBusinessStatus.ORDER_CANCELLED.getCode());
                    order.setGoodsStatus(GoodsStatusEnum.ORDER_CANCELLED.name());
                    break;
            }
            order.setUpdateTime(DateUtils.getNowDate());

            orderStatusLog = new SlkjOrderStatusLog();
            orderStatusLog.setOrderId(order.getOrderId());
            orderStatusLog.setStatus(OrderBusinessStatus.ORDER_CANCELLED.getCode());
            orderStatusLog.setChangeBy(null);
            orderStatusLog.setChangeTime(order.getUpdateTime());
            statusLogs.add(orderStatusLog);
        }
        log.info("更新订单状态：{}", JSON.toJSONString(orders));
        slkjOrderMapper.batchUpdateOrderBusinessStatus(orders);

        log.info("批量插入订单状态日志：{}", JSON.toJSONString(statusLogs));
        if (!CollectionUtils.isEmpty(statusLogs)) {
            slkjOrderStatusLogMapper.batchInsert(statusLogs);
        }
        log.info("订单过期任务执行完成");
    }

}
